package com.handinhand.mobile;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.handinhand.mobile.conn.HandInHandCallback;
import com.handinhand.mobile.model.Community;
import com.handinhand.mobile.service.CommunityService;

public class CreateCommunityActivity extends BaseActivity {

	private EditText communityName;
	private EditText communityDescription;
	private Button createCommunityButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_community);
		setupUI();
	}

	private void setupUI() {
		createCommunityButton = (Button) findViewById(R.id.btnCreateCommunity);
		createCommunityButton.setOnClickListener(onCreateCommunityButtonClick);

		communityName = (EditText) findViewById(R.id.etName);
		communityDescription = (EditText) findViewById(R.id.etDescription);		
	}
	
	private OnClickListener onCreateCommunityButtonClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String name = communityName.getText().toString();
			String description = communityDescription.getText().toString();		
			

			if (name.trim().equals("") || description.trim().equals("")) {
				Toast.makeText(CreateCommunityActivity.this, "Name and Description cannot be empty!", Toast.LENGTH_SHORT).show();
				return;
			}

			CommunityService communityService = new CommunityService(getApp());
			communityService.createCommunity(name, description, new HandInHandCallback<Community>() {
				public void onSuccess(Community response) {
					System.out.println("-------------------------------------------------------------------------" + "SUCCESS");
					Intent intent = new Intent(CreateCommunityActivity.this, HomeActivity.class);
					startActivity(intent);
					finish();
				}

				public void onFail(String errorMessage) {
					System.out.println("-------------------------------------------------------------------------" + "FAIL");
					Toast.makeText(CreateCommunityActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
				}
			});
		}
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.create_community, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
